//
//  FZVisitorView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/19/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit


class FZVisitorView: UIView {
//MARK:==========================属性==========================
    //MARK:用于回调的闭包
    var clickLoginClosure: (()->())?
    var clickResginClosure: (()->())?
    

//MARK:==========================界面==========================
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        setupLayout()
        
    }
    //MARK: 加载控件
    func setupUI() {
        
        self.backgroundColor = UIColor.init(white: 237/255, alpha: 1.0)
        addSubview(cirleImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(pointLabel)
        addSubview(resginButton)
        addSubview(loginButton)
    }
    
    //MARK:布局子控件
    func setupLayout() {
        cirleImageView .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        houseImageView .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        pointLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(self.maskImageView.snp_bottom).offset(padding)
            make.width.equalTo(padding * 23)
        }
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(pointLabel.snp_leading)
            make.top.equalTo(pointLabel.snp_bottom).offset(padding)
            make.width.equalTo(resginButton)
            make.height.equalTo(2.5 * padding)
        }
        resginButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(pointLabel.snp_trailing)
            make.leading.equalTo(loginButton.snp_trailing).offset(padding * 5)
            make.top.height.width.equalTo(loginButton)
            
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//MARK:==========================为子类设置==========================
    func setupVisitorViewInfo(imageNamed: String?, title: String?){
        if let img = imageNamed, let tit = title{
            pointLabel.text = tit;
            houseImageView.image = UIImage(named:img)
            cirleImageView.hidden = true
        }else{
            startAnimation()
        }
    }
//MARK:==========================动画==========================
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //转一圈
        anim.toValue = 2 * M_PI
        //延迟时间
        anim.duration = 10
        //重复执行
        anim.repeatCount = MAXFLOAT
        //让动画在后台切换到前台，切换到其他控制器不会停止动画
        anim.removedOnCompletion = false
        
        //给指定的空的layer 添加动画
        cirleImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
//MARK:==========================事件==========================
    
    @objc private func clickLogin(){
        clickLoginClosure?()
    }
    @objc private func clickResgin() {
        clickResginClosure?()
    }
    
//MARK:==========================懒加载==========================
    
    //MARK: 中间的图片
    lazy var cirleImageView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    lazy var maskImageView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    lazy var houseImageView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    
    //MARK: 提示文本
    lazy var pointLabel: UILabel = {
        
        let lb: UILabel = UILabel()
        lb.font = UIFont.systemFontOfSize(14)
        lb.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lb.numberOfLines = 0
        lb.textAlignment = NSTextAlignment.Center
        
        return lb
    }()
    
    //MARK: 登陆、注册按钮
    lazy var loginButton: UIButton = {
        let btn: UIButton = UIButton(title: "登陆",titleColor:UIColor.orangeColor(), backImageName: "common_button_white", target: self, action: #selector(clickLogin))
        return btn
    }()
    
    lazy var resginButton: UIButton = {
        let btn: UIButton = UIButton(title: "注册", titleColor: UIColor.orangeColor(), backImageName: "common_button_white", target: self, action: #selector(clickResgin))
        return btn
    }()

}
